#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int N = 110;
int g[N][N];
ll cnt[N];
int n, m, c, q;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }

int count_cross(int i, int j) {
  int ctr = g[i][j];
  int len = INF;
  for (int k = 0; k < 4; ++k) {
    int cur = 0;
    int nx, ny;
    while (nx = i + (cur + 1) * dx[k], ny = j + (cur + 1) * dy[k],
           valid(nx, ny) && g[nx][ny] == ctr)
      cur++;
    len = min(len, cur);
    if (len == 0) return 0;
  }
  return len;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (cin >> n >> m >> c >> q) {
    memset(cnt, 0, sizeof(cnt));
    rep(i, 0, n) rep(j, 0, m) cin >> g[i][j];
    rep(i, 0, n) rep(j, 0, m) {
      int ctr = g[i][j];
      cnt[ctr] += count_cross(i, j);
    }
    while (q--) {
      char op;
      cin >> op;
      if (op == 'Q') {
        int x;
        cin >> x;
        cout << cnt[x] << '\n';
      } else {
        int x, y, k;
        cin >> x >> y >> k;
        x--, y--;
        if (g[x][y] == k) continue;
        // Before change
        for (int i = 0; i < n; ++i) {
          int ctr = g[i][y];
          cnt[ctr] -= count_cross(i, y);
        }
        for (int i = 0; i < m; ++i) {
          int ctr = g[x][i];
          cnt[ctr] -= count_cross(x, i);
        }
        cnt[g[x][y]] += count_cross(x, y);
        // After change, recount
        g[x][y] = k;
        for (int i = 0; i < n; ++i) {
          int ctr = g[i][y];
          cnt[ctr] += count_cross(i, y);
        }
        for (int i = 0; i < m; ++i) {
          int ctr = g[x][i];
          cnt[ctr] += count_cross(x, i);
        }
        cnt[g[x][y]] -= count_cross(x, y);
      }
    }
  }
}
